package com.github.admin.edu.security.jwt.server.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/8/12
 * Time: 19:02
 */
@Entity
@Table(name = "t_sys_role_user")
public class TSysRoleUser implements Serializable {
    private Long id;
    private String userid;
    private Integer roleid;
    private Integer orgid;
    private String username;
    private String rolename;
    private String orgname;

    private TSysUser user;

    private TSysRole role;

    private TSysOrginzation orginzation;


    @Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "userid")
    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    @Basic
    @Column(name = "roleid")
    public Integer getRoleid() {
        return roleid;
    }

    public void setRoleid(Integer roleid) {
        this.roleid = roleid;
    }

    @Basic
    @Column(name = "orgid")
    public Integer getOrgid() {
        return orgid;
    }

    public void setOrgid(Integer orgid) {
        this.orgid = orgid;
    }

    @Transient
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Transient
    public String getRolename() {
        return rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }

    @Transient
    public String getOrgname() {
        return orgname;
    }

    public void setOrgname(String orgname) {
        this.orgname = orgname;
    }

    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH})
    @JoinColumn(name = "userid",referencedColumnName = "id",insertable = false,updatable = false)
    public TSysUser getUser() {
        return user;
    }

    public void setUser(TSysUser user) {
        if(null!=user){
            this.username=user.getName();
        }
        this.user = user;
    }

    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH})
    @JoinColumn(name = "roleid",referencedColumnName = "id",insertable = false,updatable = false)
    public TSysRole getRole() {
        return role;
    }

    public void setRole(TSysRole role) {
        if(null!=role){
            this.rolename=role.getName();
        }
        this.role = role;
    }

    @ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH})
    @JoinColumn(name = "orgid",referencedColumnName = "id",insertable = false,updatable = false)
    public TSysOrginzation getOrginzation() {
        return orginzation;
    }

    public void setOrginzation(TSysOrginzation orginzation) {
        if(null!=orginzation){
            this.orgname=orginzation.getName();
        }
        this.orginzation = orginzation;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TSysRoleUser that = (TSysRoleUser) o;
        return id == that.id &&
                Objects.equals(userid, that.userid) &&
                Objects.equals(roleid, that.roleid) &&
                Objects.equals(orgid, that.orgid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, userid, roleid, orgid);
    }
}
